/*
 * Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.xuexiang.templateproject.R;
import com.xuexiang.templateproject.bean.Box;
import com.xuexiang.templateproject.bean.ImageBoxEntity;
import com.xuexiang.templateproject.bean.LeftBottom;
import com.xuexiang.templateproject.bean.LeftTop;
import com.xuexiang.templateproject.bean.RightBottom;
import com.xuexiang.templateproject.bean.RightTop;

import java.util.concurrent.ExecutionException;

/* loaded from: classes2.dex */
public class RectView extends BaseView {
    public Paint B;
    public RectF C;
    public RectF D;

    /* renamed from: c  reason: collision with root package name */
    public float f4489c;

    /* renamed from: d  reason: collision with root package name */
    public float f4490d;

    /* renamed from: e  reason: collision with root package name */
    public float f4491e;

    /* renamed from: f  reason: collision with root package name */
    public float f4492f;

    /* renamed from: g  reason: collision with root package name */
    public float f4493g;

    /* renamed from: h  reason: collision with root package name */
    public float f4494h;

    /* renamed from: i  reason: collision with root package name */
    public float f4495i;

    /* renamed from: j  reason: collision with root package name */
    public float f4496j;

    /* renamed from: k  reason: collision with root package name */
    public float f4497k;

    /* renamed from: l  reason: collision with root package name */
    public float f4498l;

    /* renamed from: m  reason: collision with root package name */
    public Path f4499m;

    /* renamed from: n  reason: collision with root package name */
    public Path f4500n;

    /* renamed from: o  reason: collision with root package name */
    public Path f4501o;

    /* renamed from: p  reason: collision with root package name */
    public Path f4502p;
    public Path q;
    public RectF r;
    public RectF s;
    public Matrix t;
    public Bitmap u;
    public Point v;
    public int w;
    public int x;
    public ImageBoxEntity y;
    public Box z;
    public int A = 0;
    public float E = 0.0f;

    /* loaded from: classes2.dex */
    public class a implements Runnable {

        /* renamed from: com.jcds.learneasy.widget.RectView$a$a  reason: collision with other inner class name */
        /* loaded from: classes2.dex */
        public class RunnableC0047a implements Runnable {
            public RunnableC0047a() {
            }

            @Override // java.lang.Runnable
            public void run() {
                RectView.this.invalidate();
            }
        }

        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            RectView rectView = RectView.this;
            Glide.with(rectView.getContext())
                    .load(RectView.this.y.getDewarpImgUrl())
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            BitmapDrawable bd = (BitmapDrawable) resource;
                            rectView.u = bd.getBitmap();
                            ((AppCompatActivity) RectView.this.getContext()).runOnUiThread(new RunnableC0047a());
                        }
                    })
                 ;

        }
    }

    public RectView(Context context, ImageBoxEntity imageBoxEntity, Box box) {
        super(context);
        i(imageBoxEntity, box);
    }

    private View get() {
        return this;
    }

    public final int[] c() {
        int i2;
        int i3;
        RectF oval = getOval();
        if (oval == null) {
            return new int[]{0, 0};
        }
        float f2 = this.f4493g;
        float f3 = this.f4495i;
        float f4 = f2 - oval.left;
        float f5 = oval.right - f3;
        if (f4 > 0.0f) {
            i2 = -Math.round(f4);
        } else {
            i2 = f5 > 0.0f ? Math.round(f5) : 0;
        }
        float f6 = this.f4494h;
        float f7 = this.f4496j;
        float f8 = f6 - oval.top;
        float f9 = oval.bottom - f7;
        if (f8 > 0.0f) {
            i3 = -Math.round(f8);
        } else {
            i3 = f9 > 0.0f ? Math.round(f9) : 0;
        }
        return new int[]{i2, i3};
    }

    public final int d() {
        float f2 = this.f4489c;
        float f3 = this.f4490d;
        RectF rectF = this.C;
        if (k(f2, f3, rectF.left, rectF.top)) {
            return 1;
        }
        float f4 = this.f4489c;
        float f5 = this.f4490d;
        RectF rectF2 = this.C;
        if (k(f4, f5, rectF2.left, rectF2.bottom)) {
            return 4;
        }
        float f6 = this.f4489c;
        float f7 = this.f4490d;
        RectF rectF3 = this.C;
        if (k(f6, f7, rectF3.right, rectF3.top)) {
            return 2;
        }
        float f8 = this.f4489c;
        float f9 = this.f4490d;
        RectF rectF4 = this.C;
        return k(f8, f9, rectF4.right, rectF4.bottom) ? 3 : 0;
    }

    public final void e(Canvas canvas) {
        RectF rectF = this.r;
        RectF rectF2 = this.C;
        float f2 = rectF2.left;
        float f3 = rectF2.bottom;
        rectF.set(f2, f3 - this.f4498l, this.f4497k + f2, f3);
        RectF rectF3 = this.s;
        RectF rectF4 = this.C;
        float f4 = rectF4.left;
        float f5 = rectF4.bottom;
        rectF3.set(f4, f5 - this.f4497k, this.f4498l + f4, f5);
        this.f4500n.rewind();
        this.f4501o.rewind();
        this.f4499m.rewind();
        this.f4500n.addRect(this.r, Path.Direction.CCW);
        this.f4501o.addRect(this.s, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.f4499m.op(this.f4500n, this.f4501o, Path.Op.UNION);
        }
        canvas.drawPath(this.f4499m, this.B);
    }

    public final void f(Canvas canvas) {
        RectF rectF = this.r;
        RectF rectF2 = this.C;
        float f2 = rectF2.left;
        float f3 = rectF2.top;
        rectF.set(f2, f3, this.f4497k + f2, this.f4498l + f3);
        RectF rectF3 = this.s;
        RectF rectF4 = this.C;
        float f4 = rectF4.left;
        float f5 = rectF4.top;
        rectF3.set(f4, f5, this.f4498l + f4, this.f4497k + f5);
        this.f4500n.rewind();
        this.f4501o.rewind();
        this.f4499m.rewind();
        this.f4500n.addRect(this.r, Path.Direction.CCW);
        this.f4501o.addRect(this.s, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.f4499m.op(this.f4500n, this.f4501o, Path.Op.UNION);
        }
        canvas.drawPath(this.f4499m, this.B);
    }

    public final void g(Canvas canvas) {
        RectF rectF = this.r;
        RectF rectF2 = this.C;
        float f2 = rectF2.right;
        float f3 = rectF2.bottom;
        rectF.set(f2 - this.f4497k, f3 - this.f4498l, f2, f3);
        RectF rectF3 = this.s;
        RectF rectF4 = this.C;
        float f4 = rectF4.right;
        float f5 = rectF4.bottom;
        rectF3.set(f4 - this.f4498l, f5 - this.f4497k, f4, f5);
        this.f4500n.rewind();
        this.f4501o.rewind();
        this.f4499m.rewind();
        this.f4500n.addRect(this.r, Path.Direction.CCW);
        this.f4501o.addRect(this.s, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.f4499m.op(this.f4500n, this.f4501o, Path.Op.UNION);
        }
        canvas.drawPath(this.f4499m, this.B);
    }

    public Box getBox() {
        RectF rectF = this.C;
        float f2 = rectF.left;
        float f3 = rectF.right;
        float f4 = rectF.top;
        float f5 = rectF.bottom;
        float f6 = f2 - this.f4493g;
        float f7 = this.w - (this.f4495i - f3);
        float f8 = f4 - this.f4494h;
        float f9 = this.x - (this.f4496j - f5);
        return new Box(this.z.getSubTaskId(), this.z.getCuotiNo(), new LeftBottom(f6 / this.w, f9 / this.x), new LeftTop(f6 / this.w, f8 / this.x), new RightBottom(f7 / this.w, f9 / this.x), new RightTop(f7 / this.w, f8 / this.x), false, this.z.getBoxId());
    }

    public RectF getOval() {
        return this.C;
    }

    public final void h(Canvas canvas) {
        RectF rectF = this.r;
        RectF rectF2 = this.C;
        float f2 = rectF2.right;
        float f3 = rectF2.top;
        rectF.set(f2 - this.f4497k, f3, f2, this.f4498l + f3);
        RectF rectF3 = this.s;
        RectF rectF4 = this.C;
        float f4 = rectF4.right;
        float f5 = rectF4.top;
        rectF3.set(f4 - this.f4498l, f5, f4, this.f4497k + f5);
        this.f4500n.rewind();
        this.f4501o.rewind();
        this.f4499m.rewind();
        this.f4500n.addRect(this.r, Path.Direction.CCW);
        this.f4501o.addRect(this.s, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.f4499m.op(this.f4500n, this.f4501o, Path.Op.UNION);
        }
        canvas.drawPath(this.f4499m, this.B);
    }

    public final void i(ImageBoxEntity imageBoxEntity, Box box) {
        this.y = imageBoxEntity;
        this.z = box;
        new Thread(new a()).start();
    }

    public final float[] j() {
        RectF rectF = this.C;
        float f2 = rectF.left;
        float f3 = f2 + ((rectF.right - f2) / 2.0f);
        float f4 = rectF.top;
        float f5 = f4 + ((rectF.bottom - f4) / 2.0f);
        Point point = this.v;
        return new float[]{point.x - f3, point.y - f5};
    }

    public boolean k(float f2, float f3, float f4, float f5) {
        float abs = Math.abs(f2 - f4);
        float abs2 = Math.abs(f3 - f5);
        return Math.pow((double) ((abs * abs) + (abs2 * abs2)), 0.5d) <= ((double) this.E);
    }

    public final float l(float f2) {
        int i2;
        int i3 = this.v.x;
        int i4 = this.w;
        if (f2 < i3 - (i4 / 2)) {
            i2 = i3 - (i4 / 2);
        } else if (f2 <= (i4 / 2) + i3) {
            return f2;
        } else {
            i2 = i3 + (i4 / 2);
        }
        return i2;
    }

    public final float m(float f2) {
        int i2;
        int i3 = this.v.y;
        int i4 = this.x;
        if (f2 < i3 - (i4 / 2)) {
            i2 = i3 - (i4 / 2);
        } else if (f2 <= (i4 / 2) + i3) {
            return f2;
        } else {
            i2 = i3 + (i4 / 2);
        }
        return i2;
    }

    public final Bitmap n(Bitmap bitmap, float f2) {
        if (bitmap == null) {
            return null;
        }
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.postScale(f2, f2);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    }

    @Override // android.view.View
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = this.u;
        if (bitmap != null) {
            Bitmap n2 = n(this.u, (this.f4375a * 1.0f) / (bitmap.getWidth() * 1.0f));
            this.u = n2;
            canvas.drawBitmap(n2, this.t, this.B);
        }
        this.B.setStyle(Paint.Style.FILL);
        this.B.setColor(getResources().getColor(R.color.white_overlay));
        this.f4499m.rewind();
        this.f4502p.rewind();
        this.q.rewind();
        this.f4502p.addRect(this.C, Path.Direction.CCW);
        this.q.addRect(this.D, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.f4499m.op(this.q, this.f4502p, Path.Op.DIFFERENCE);
        }
        canvas.drawPath(this.f4499m, this.B);
        this.B.setStyle(Paint.Style.STROKE);
        this.B.setColor(-16776961);
        this.B.setStrokeCap(Paint.Cap.ROUND);
        this.B.setStrokeWidth(1.0f);
        canvas.drawRect(this.C, this.B);
        this.B.setStrokeWidth(10.0f);
        this.B.setStyle(Paint.Style.FILL);
        f(canvas);
        h(canvas);
        g(canvas);
        e(canvas);
    }

    @Override // com.jcds.learneasy.widget.BaseView, android.view.View
    public void onSizeChanged(int i2, int i3, int i4, int i5) {
        super.onSizeChanged(i2, i3, i4, i5);
        int i6 = this.f4375a;
        int floatValue = (int) ((Float.valueOf(i6).floatValue() / Float.valueOf(this.y.getWidth()).floatValue()) * Float.valueOf(this.y.getHeight()).floatValue());
        this.t = new Matrix();
        this.w = i6;
        this.x = floatValue;
        this.v = new Point(this.f4375a / 2, this.f4376b / 2);
        Point point = new Point(this.w / 2, this.x / 2);
        Matrix matrix = this.t;
        Point point2 = this.v;
        matrix.postTranslate(point2.x - point.x, point2.y - point.y);
        Point point3 = this.v;
        int i7 = point3.x;
        int i8 = this.w;
        this.f4493g = i7 - (i8 / 2);
        int i9 = point3.y;
        int i10 = this.x;
        this.f4494h = i9 - (i10 / 2);
        this.f4495i = i7 + (i8 / 2);
        this.f4496j = i9 + (i10 / 2);
        this.E = Math.min(this.f4375a, this.f4376b) / 10;
        this.f4497k = 30.0f;
        this.f4498l = 8.0f;
        Paint paint = new Paint();
        this.B = paint;
        paint.setAntiAlias(true);
        this.D = new RectF();
        this.C = new RectF();
        double d2 = i6;
        double d3 = floatValue;
        float y = (float) (this.z.getLeftTop().getY() * d3);
        float y2 = (float) (d3 * this.z.getRightBottom().getY());
        RectF rectF = this.C;
        int i11 = this.v.y;
        int i12 = this.x;
        rectF.set((float) (this.z.getLeftTop().getX() * d2), (i11 - (i12 / 2)) + y, (float) (d2 * this.z.getRightTop().getX()), (i11 - (i12 / 2)) + y2);
        this.D.set(0.0f, 0.0f, this.f4375a, this.f4376b);
        RectF rectF2 = this.C;
        rectF2.set(rectF2);
        this.f4499m = new Path();
        this.q = new Path();
        this.f4502p = new Path();
        this.f4500n = new Path();
        this.f4501o = new Path();
        this.r = new RectF();
        this.s = new RectF();
    }

    /* JADX WARN: Code restructure failed: missing block: B:7:0x0019, code lost:
        if (r0 != 3) goto L_0x018c;
     */
    @Override // android.view.View
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public boolean onTouchEvent(android.view.MotionEvent r8) {
        /*
            Method dump skipped, instructions count: 400
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.jcds.learneasy.widget.RectView.onTouchEvent(android.view.MotionEvent):boolean");
    }
}
